<?php


namespace App\Dao\System;


use App\Dao\BaseDao;
use App\Model\System\SystemMenu;
use Hyperf\Database\Model\Builder;
use Hyperf\Utils\Collection;
use PhpParser\Node\Expr\Array_;
use Yj\Daos\Verify;

/**
 * @var SystemMenu
 */
class SystemMenuDao extends \Yj\Daos\BaseDaos
{
    public function DaoWhere(Collection $params)
    {
            $search = $this->getSearch();
            $params->check('id',function ($id)use($search){
                $search->where('id',$id);
            });
            $params->check('admin_id',function ($admin_id)use($search){
                if($admin_id!=1){
                    //获取你的角色
                    $search->whereIn('id',$this->getAdminMenusId($admin_id));
                }
            });
            return $search;
    }

    public function getAdminMenusId($adminId):Array
    {
        $rolesId = App(SystemAdminDao::class)->getRolesId($adminId);
        $menu_ids =  App(SystemRolesMenuDao::class)->where('roles_id',$rolesId)
            ->pluck('menu_id')->toArray();
        return $menu_ids;
    }

    public function check($id, Collection $params)
    {
        if ($path = $params->get('path')) {
            if ($this->where('id', '<>', $id)
                ->where('path', $params->get('path'))
                ->exists()) {
                _Error('path is exists');
            }
        }
    }
}